from flask import Flask, Response, jsonify, request
import os
app = Flask(__name__)

def root_dir():  # pragma: no cover
    return os.path.abspath(os.path.dirname(__file__))

def get_file(filename):  # pragma: no cover
    try:
        src = os.path.join(root_dir(), filename)
        # Figure out how flask returns static files
        # Tried:
        # - render_template
        # - send_file
        # This should not be so non-obvious
        return open(src).read()
    except IOError as exc:
        return str(exc)

@app.route('/')
def hello_world():
    content = get_file('index.html')
    return Response(content, mimetype="text/html")

@app.route('/lib/<path:path>')
def get_resource(path):  # pragma: no cover
    mimetypes = {
        ".css": "text/css",
        ".html": "text/html",
        ".js": "application/javascript",
    }
    path = 'lib/' + path
    complete_path = os.path.join(root_dir(), path)
    ext = os.path.splitext(path)[1]
    mimetype = mimetypes.get(ext, "text/html")
    content = get_file(complete_path)
    return Response(content, mimetype=mimetype)

@app.route('/api/info.lua')
def get_info():
    return jsonify({
        'user': 'dzlua',
        'cid': 123456,
        'mfree': 1024*30,
        'muse': 1024*60,
        'fid': 123456,
        'fsize': 1024*1024*4,
        'fmode': 2,
        'fspeed': 40*1000000,
        'amac': '12-34-56-78-91-12',
        'smac': '22-34-56-78-91-12',
        'ver': '2.0.0'
    })

sta_info = {}
@app.route('/api/sta.lua', methods=['GET', 'PUT'])
def get_sta():
    if request.method == 'PUT':
        sta_info['ssid'] = request.form.get('ssid')
        sta_info['pwd'] = request.form.get('pwd')
    return jsonify(sta_info)

ap_info = {}
@app.route('/api/ap.lua', methods=['GET', 'PUT'])
def get_ap():
    if request.method == 'PUT':
        ap_info['ssid'] = request.form.get('ssid')
        ap_info['pwd'] = request.form.get('pwd')
        ap_info['ip'] = request.form.get('ip')
        ap_info['netmask'] = request.form.get('netmask')
        ap_info['gateway'] = request.form.get('gateway')
    return jsonify(ap_info)

ap_list = {'status':1}
@app.route('/api/scanap.lua')
def get_scanap():
    if request.args.get('update') == True:
        ap_list['status'] = 1
        return jsonify(ap_list)

    if ap_list['status'] == 1:
        ap_list['status'] = 0
        ap_list['data'] = {
            'esp00':{'rssi':-100},
            'esp01':{'rssi':-90},
            'esp02':{'rssi':-80},
            'esp03':{'rssi':-70},
            'esp04':{'rssi':-60},
            'esp05':{'rssi':-50},
            'esp06':{'rssi':-40},
            'esp07':{'rssi':-30},
            'esp08':{'rssi':-20},
            'esp09':{'rssi':-10},
            'esp10':{'rssi':0},
            'esp11':{'rssi':10},
            'esp12':{'rssi':20},
            'esp13':{'rssi':30},
            'esp14':{'rssi':40},
            'esp15':{'rssi':50},
            'esp16':{'rssi':60},
            'esp17':{'rssi':70},
            'esp18':{'rssi':80},
            'esp19':{'rssi':90},
            'esp20':{'rssi':100},
        }
    return jsonify(ap_list)

if __name__ == '__main__':
    app.run(debug=True)